$(document).ready(function () {
    init();
    initFenlei();
});

function initFenlei() {
    $.ajax({
        url: "/fenlei/findAll",
        type: 'GET',
        data: {},
        dataType: "JSON",
        async: false,
        success: function (res) {

            if (res.code === "200") {
                var data = res.data;
                var xlk = "<option value=''>所有分类</option>";
                for (var i = 0; i < data.length; i++) {
                    xlk += "<option class='' value=" + data[i].name + ">" + data[i].name + "</option>";
                }
                $(".contenta").html(xlk);
            } else {
                alert(res.msg)
            }
        },
        error: function (e) {
            alert('发生错误')
        }

    })
}

function update() {
    var path = location.href;
    var id = path.substring(path.lastIndexOf("/") + 1)
    var content = $(".contenta").val();
    var name = $("input[name='name']").val();
    var price = $("input[name='price']").val();

    $.ajax({
        url: "/product/updateById",
        type: 'GET',
        data: {
            'id': id,
            'content': content,
            'name': name,
            'price': price
        },
        dataType: "JSON",
        async: false,
        success: function (res) {
            if (res.code == "201") {
                alert(res.msg);
            }
            window.location.href = "/productlist";
        },
        error: function (e) {
            alert('发生错误')
        }
    })
}


function init() {

    var path = location.href;
    var id = path.substring(path.lastIndexOf("/") + 1)

    $.ajax({
        url: "/product/findById",
        type: 'GET',
        data: {
            'id': id
        },
        dataType: "JSON",
        async: false,
        success: function (res) {
            if (res.code == "200") {
                var data = res.data;
                $(".id").val(data.id);
                $(".name").val(data.name);
                $(".price").val(data.price);
            } else {
                alert(res.msg);
            }
        },
        error: function (e) {
            alert('发生错误')
        }
    })
}